package com.oms.web.comet4j;


import java.util.Random;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.comet4j.core.CometContext;
import org.comet4j.core.CometEngine;

public class HelloWorld implements ServletContextListener {
        private static final String CHANNEL = "hello";
        public void contextInitialized(ServletContextEvent arg0) {
                CometContext cc = CometContext.getInstance();
                cc.registChannel(CHANNEL);
                Thread helloAppModule = new Thread(new HelloAppModule(), "Sender App Module");
                helloAppModule.setDaemon(true);
                helloAppModule.start();
        }

/*        class HelloAppModule implements Runnable {
                public void run() {
                        while (true) {
                                try {
                                        Thread.sleep(1000);
                                } catch (Exception ex) {
                                        ex.printStackTrace();
                                }
                                CometEngine engine = CometContext.getInstance().getEngine();
                                engine.sendToAll(CHANNEL, getName());
                        }
                }
        }

        
        public String getName(){
        	String[]names = new String[]{"冯春雷","吴兆元","夏胜安","李瑞东","田东东","张文良","刘权","邵帅","肖小良","任银","王继伟"};
        	int number = new Random().nextInt(10) + 1;
        	return names[number];
        }*/
        

        class HelloAppModule implements Runnable {
                public void run() {
                        while (true) {
                                try {
                                        Thread.sleep(1000);
                                } catch (Exception ex) {
                                        ex.printStackTrace();
                                }
                                CometEngine engine = CometContext.getInstance().getEngine();
                                engine.sendToAll(CHANNEL, Runtime.getRuntime().freeMemory()/1024);
                        }
                }
        }

        public void contextDestroyed(ServletContextEvent arg0) {

        }
}